from django.shortcuts import redirect, render
from main.models import *
from django.http import HttpResponse
import threading
from pachong.get_up import get_up_data,get_video_image
from pyecharts.charts import Line
from pyecharts import options as opts

# Create your views here.
def index(request):
    return redirect('/index/')

def hello(request):
    Up_list = Up.objects.all().order_by('-fans_num')[:3]
    data = []
    for up in Up_list:
        data.append({'name':up.name,'url':up.url,'image':up.image,'fans_num':up.fans_num,'fans':up.fans,"id":up.id})
    return render(request, 'index.html', {"data":data})


def up_submit(request):
    if request.method == 'POST':
        Up_name = request.POST.get('UP_name')
        Up_url = request.POST.get('url')
        if(Up.objects.filter(url=Up_url,name=Up_name).exists()):
            return HttpResponse("<script>alert('该UP主已存在');window.location.href='/index/';</script>")
        threading.Thread(target=get_up_data,args=(Up_name,Up_url,)).start()
        return HttpResponse("<script>alert('上传成功');window.location.href='/index/';</script>")
    else:
        return HttpResponse("上传失败")


def up_detail(request):
    id = request.GET.get('id')
    up = Up.objects.get(id=id)
    data = {'name':up.name,'url':up.url,'image':up.image,'fans_num':up.fans_num,'fans':up.fans}
    no_image = Video.objects.filter(upid=id,image__isnull=True) 
    if len(no_image) > 0:
        threading.Thread(target=get_video_image,args=(no_image,)).start()
    videos = Video.objects.filter(upid=id).order_by('-time').values()

    videos = list(videos)   
    return render(request, 'up_detail.html', {"data":data,"videos":videos})


def video_detail(request):
    id = request.GET.get('id')
    video = Video.objects.get(id=id)
    video_data = VideoData.objects.filter(videoid=id).order_by('time').values()
    video_data = list(video_data)
    # 画图 视频数据 其中有视频播放量、分享数、点赞数、收藏数随时间的变化
    line = Line(init_opts={"width": "900px", "height": "500px"})
    line.set_global_opts(
    title_opts={"text": "视频数据变化图",},
    xaxis_opts={"name": "日期", "name_location": "middle", "name_gap": 20, "axislabel_opts": {"rotate": 45}},
    yaxis_opts={"name": "播放量", "axislabel_opts": {"formatter": "{value}"}},
    legend_opts={"pos_top": "10%", "pos_left": "center"},
    tooltip_opts={"trigger": "axis"},  # 鼠标悬浮提示框
    datazoom_opts=[
        {"type": "inside"},  # 内部缩放
        {"type": "slider", "start": 0, "end": 10}  # 外部滑动条
    ]
)
    times = [i['date'] for i in video_data]
    views = [i['views'] for i in video_data]
    shares = [i['shares'] for i in video_data]
    likes = [i['likes'] for i in video_data]
    collections = [i['collections'] for i in video_data]
    line.add_xaxis(times)
    line.add_yaxis("播放量", views, color="#5470C6", yaxis_index=0,label_opts=opts.LabelOpts(is_show=False))

# 设置第二个 Y 轴，添加其他的数据系列
    line.extend_axis(
        yaxis=opts.AxisOpts(name="其他数据", min_=0)  # 其他数据的 Y 轴
    )

    # 添加分享数、点赞数和收藏数到第二个 Y 轴
    line.add_yaxis("分享数", shares, color="#91CC75", yaxis_index=1,label_opts=opts.LabelOpts(is_show=False))
    line.add_yaxis("点赞数", likes, color="#FAC858", yaxis_index=1,label_opts=opts.LabelOpts(is_show=False))
    line.add_yaxis("收藏数", collections, color="#EE6666", yaxis_index=1,label_opts=opts.LabelOpts(is_show=False))
    data = {'name':video.name,'url':video.url,'image':video.image,'date':video.date}
    content =dict(
        data=data,
        myechart=line.render_embed()
    )
    
    return render(request, 'video_detail.html', content)


def all_up(request):
    Up_list = Up.objects.all().order_by('-fans_num').values()
    Up_list = list(Up_list)
    return render(request, 'all_up.html', {"Up_list":Up_list})